#!/usr/bin/env bash
source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/config"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_BASE_PATH/common/functions"
source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/functions"

service-backup-auth-cmd() {
  #E setup s3 backup authentication
  #E dokku $PLUGIN_COMMAND_PREFIX:backup-auth lolipop AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY
  #E setup s3 backup authentication with different region
  #E dokku $PLUGIN_COMMAND_PREFIX:backup-auth lolipop AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_REGION
  #E setup s3 backup authentication with different signature version and endpoint
  #E dokku $PLUGIN_COMMAND_PREFIX:backup-auth lolipop AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_REGION AWS_SIGNATURE_VERSION ENDPOINT_URL
  #E more specific example for minio auth
  #E dokku $PLUGIN_COMMAND_PREFIX:backup-auth lolipop MINIO_ACCESS_KEY_ID MINIO_SECRET_ACCESS_KEY us-east-1 s3v4 https://YOURMINIOSERVICE
  #A service, service to run command against
  #A access-key-id, an amazon AWS_ACCESS_KEY_ID
  #A aws-secret-access-key, an amazon AWS_SECRET_ACCESS_KEY
  #A aws-default-region, (optional) a valid amazon S3 region
  #A aws-signature-version, (optional) the AWS signature version to use when signing S3 requests
  #A endpoint-url, (optional) an aws endpoint to upload to
  declare desc="sets up authentication for backups on the $PLUGIN_SERVICE service"
  local cmd="$PLUGIN_COMMAND_PREFIX:backup-auth" argv=("$@")
  [[ ${argv[0]} == "$cmd" ]] && shift 1
  declare SERVICE="$1" AWS_ACCESS_KEY_ID="$2" AWS_SECRET_ACCESS_KEY="$3" AWS_DEFAULT_REGION="$4" AWS_SIGNATURE_VERSION="$5" ENDPOINT_URL="$6"
  is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented"

  [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a valid name for the service"
  [[ -z "$AWS_ACCESS_KEY_ID" ]] && dokku_log_fail "Please specify an aws access key id"
  [[ -z "$AWS_SECRET_ACCESS_KEY" ]] && dokku_log_fail "Please specify an aws secret access key"
  verify_service_name "$SERVICE"
  service_backup_auth "$SERVICE" "$AWS_ACCESS_KEY_ID" "$AWS_SECRET_ACCESS_KEY" "$AWS_DEFAULT_REGION" "$AWS_SIGNATURE_VERSION" "$ENDPOINT_URL"
}

service-backup-auth-cmd "$@"
